package org.lendoo.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.lendoo.web.entity.Image;
import org.lendoo.web.enumeration.ServiceResultEnum;
import org.lendoo.web.pojo.common.ServiceResult;
import org.lendoo.web.pojo.dto.RelCategoryAndImageUrlListDto;
import org.springframework.stereotype.Service;
import org.lendoo.web.mapper.CategoryMapper;
import org.lendoo.web.entity.Category;
import org.lendoo.web.service.CategoryService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * CategoryService接口的实现类
 */
@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    
    @Resource
    private ServiceHelper serviceHelper;

    /**
     * 新增
     * @param category 分类表
     * @return 添加成功返回1，添加失败返回-1
     */
    @Override
    public ServiceResult<Boolean> add(Category category) {
        baseMapper.insert(category);
        return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 批量导入
     * @param categoryList 分类表（列表）
     * @return 批量导入成功返回1，批量导入失败返回-1
     */
    @Override
    public ServiceResult<Boolean> batchAdd(List<Category> categoryList) {
        baseMapper.insertBatchSomeColumn(categoryList);
        return new ServiceResult<>(ServiceResultEnum.INSERT_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.INSERT_SUCCESS.getMessage());
    }

    /**
     * 根据id更新
     * @param category 分类表
     * @return 更新成功返回1，更新失败返回-1
     */
    @Override
    public ServiceResult<Boolean> updateForId(Category category) {
        baseMapper.updateById(category);
        return new ServiceResult<>(ServiceResultEnum.UPDATE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.UPDATE_SUCCESS.getMessage());
    }

    /**
     * 根据id删除
     * @param id 主键
     * @return 删除成功返回1，删除失败返回-1
     */
    @Override
    public ServiceResult<Boolean> deleteById(Serializable id) {
        baseMapper.deleteById(id);
        return new ServiceResult<>(ServiceResultEnum.DELETE_SUCCESS.getCode(), Boolean.TRUE, Boolean.TRUE, ServiceResultEnum.DELETE_SUCCESS.getMessage());
    }

    /**
     * 根据id查询
     * @param id 主键
     * @return 返回查询结果
     */
    @Override
    public ServiceResult<Category> selectById(Serializable id) {
        Category category = baseMapper.selectById(id);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), category, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 根据条件，分页查询，升序/降序排列
     * @param category 分类表
     * @param pageNo 页号
     * @param pageSize 每页行数
     * @return 返回查询结果列表
     */
    @Override
    public ServiceResult<PageInfo<Category>> page(Category category, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Category> categoryList = baseMapper.selectList(lambdaQueryWrapper);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), new PageInfo<>(categoryList), Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 查询所有记录
     * @return
     */
    @Override
    public ServiceResult<List<Category>> findAll() {
        List<Category> categoryList = baseMapper.selectList(null);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), categoryList, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 返回顶级分类列表
     * @return
     */
    @Override
    public ServiceResult<List<Category>> findTopCategory() {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.isNull(Category::getParentId);
        List<Category> categoryList = baseMapper.selectList(lambdaQueryWrapper);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), categoryList, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 查找子集分类列表
     * @param parentId
     * @return
     */
    @Override
    public ServiceResult<List<Category>> findSubCategory(Integer parentId) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getParentId, parentId);
        List<Category> categoryList = baseMapper.selectList(lambdaQueryWrapper);
        return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), categoryList, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
    }

    /**
     * 根据父级分类的id，查找子集分类列表和对应的图片列表
     * @param parentCategoryId
     * @return
     */
    @Override
    public ServiceResult<List<RelCategoryAndImageUrlListDto>> findSubCategoryListAndImageListByParentCategoryId(Integer parentCategoryId) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getParentId, parentCategoryId);
        List<Category> categoryList = serviceHelper.getCategoryMapper().selectList(lambdaQueryWrapper);

        List<RelCategoryAndImageUrlListDto> relCategoryAndImageUrlListDtoList = new ArrayList<>();
        if (null != categoryList && categoryList.size() > 0) {
            for (Category category : categoryList){
                List<Image> imageList = serviceHelper.getRelCategoryImageMapper().findByCategoryId(category.getId());
                RelCategoryAndImageUrlListDto relCategoryAndImageUrlListDto = new RelCategoryAndImageUrlListDto();
                relCategoryAndImageUrlListDto.setCategory(category);
                relCategoryAndImageUrlListDto.setImageList(imageList);
                relCategoryAndImageUrlListDtoList.add(relCategoryAndImageUrlListDto);
            }
            return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), relCategoryAndImageUrlListDtoList, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
        } else {
            return new ServiceResult<>(ServiceResultEnum.SELECT_SUCCESS.getCode(), null, Boolean.TRUE, ServiceResultEnum.SELECT_SUCCESS.getMessage());
        }
    }
}
